 

import Model.*;

/**
 * Trade controller
 * @author Stefan
 */
public class TradeController {
    //Attributes

    private AssociateController assCtr = AssociateController.getInstance();
    private static TradeController instance = new TradeController();
    private ProductController proCtr = ProductController.getInstance();
    TradeContainer container;

    private TradeController()
    {
        this.container = TradeContainer.getInstance();
    }

    /**
     * Singleton
     * @return Instance of TradeController
     */
    public static TradeController getInstance()
    {
        return instance;
    }

    /**
     * Add Trade(Instant buy)
     * @param id
     * @return true, if added succesfully
     */
    public boolean addTrade(String id, String employeeID)
    {
        boolean added = false;
        if (container.findTradeID(id) == null)
        {
            if(assCtr.findEmployeeID(employeeID) != null)
            {
                Trade trade = new Trade(id, employeeID, null);
                container.addTrade(trade);
                added = true;
            }
        }
        return added;
    }

    /**
     * Add Customer to Trade
     * @param ID
     * @return true, if added succesfully
     */
    public boolean addCustomer(String customerID, String ID)
    {
        boolean added = false;
        Trade trade = findTrade(ID);
        if(trade != null)
        {
            if (assCtr.findCustomerID(customerID) != null)
            {
                trade.setCustomerID(customerID);
                added = true;
            }
        }
        return added;
    }

    /**
     * Add Order
     * @param id
     * @param date
     * @param address
     * @param empId
     * @param cusId
     * @return int, 0 if there was no employee with typed ID, 1 if employee is found but already an order with the same ID, 2 if no customer with typed ID, 3 if added succesfully.
     */
    public int addOrder(String id, String date, String address, String empId, String cusId)
    {
        int status = 0;
        if (assCtr.findEmployeeID(empId) != null)
        {
            status++;
            if (assCtr.findCustomerID(cusId) != null)
            {
                status++;
                if (findOrder(id) == null)
                {
                    Order order = new Order(id, date, address, empId, cusId);
                    container.addTrade(order);
                    status++;
                }
            }
        }
        return status;
    }

    /**
     * Find Trade by id
     * @param id
     * @return Trade
     */
    public Trade findTrade(String id)
    {
        return container.findTradeID(id);
    }

    /**
     * Find Order by id
     * @param id
     * @return Order
     */
    public Order findOrder(String id)
    {
        return container.findOrderID(id);
    }

    /**
     * Find rent by id
     * @param id
     * @return Rent
     */
    public Rent findRent(String id)
    {
        return container.findRentID(id);
    }

    /**
     * Delete Trade by id
     * @param id
     * @return true, if deleted succesfully
     */
    public boolean deleteTrade(String id)
    {
        boolean deleted = false;
        Trade trade = findTrade(id);
        if (trade != null)
        {
            container.deleteTrade(trade);
            deleted = true;
        }
        return deleted;
    }

    /**
     * Add SaleLinesItem to a Trade and check if it already exsists
     * @param productID
     * @param quantity
     * @param tradeID
     * @return true, if added succesfully
     */
    public boolean addSaleItem(String productID, int quantity, String tradeID)
    {
        boolean added = false;
        Product pro = proCtr.findProduct(productID);
        Trade trade = findTrade(tradeID);
        //If product don't exist return false
        if (pro != null)
        {
            //If no copies of product return false
            if (pro.copies() > 0)
            {
                //If no Salelineitems add a new one if product ID exists
                if (trade.getSize() <= 0)
                {
                    if (pro.getId() != null)
                    {
                        //Create new SaleLineItem, if added succesfully boolean added = true
                        SalesLineItem newItem = new SalesLineItem(pro, quantity);
                        added = trade.addSalesLineItem(newItem);
                    }
                    else
                    {
                        added = false;
                    }
                }
                //If trade has SaleLinesItems, check all SalesLineItems
                else
                {
                    for (SalesLineItem item : trade.getSaleLines())
                    {
                        //If product already exists on trade then edit quantity
                        if (item.getProduct().getId().equals(pro.getId()))
                        {
                            item.addQuantity(quantity);
                            added = true;
                        }
                        //If product dont exists add new one
                        else
                        {
                            SalesLineItem newItem = new SalesLineItem(pro, quantity);
                            added = trade.addSalesLineItem(newItem);
                        }
                    }
                }
            }
        }
        return added;
    }
}
